/*
* $Id: ChangePointView.cpp 68 2010-03-18 19:49:39Z volok-aleksej@yandex.ru $
*
* Copyright 2009 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "ChangePointView.h"
#include "ViewFieldValuesHelper.h"
#include "Visual/Drawers/EditorDrawer/HeaderItem.h"
#include "Visual/Drawers/EditorDrawer/EditItem.h"
#include "Database/Tables/PointsTable.h"
#include "Database/Operation/ChangePointOperation.h"
#include "Settings/Journal/PointViewSettings.h"
#include "Settings/DinIntDefined.h"
#include "Settings/SettingControlIds.h"

#define TITLE_ID		1
#define NAME_POINT_ID	2
#define NOTE_POINT_ID	3
#define USER_FIELD_ID	4

CChangePointView::CChangePointView(CState* pState)
: CAddChangeView(pState), m_nUserFields(0){}
CChangePointView::~CChangePointView(){}

void CChangePointView::Init(){
	m_editorDrawer.Clear();
	m_editorDrawer.AddItem(new CHeaderItem(IApplication::GetApp()->GetLanguageManager()->GetString(CHANGE_POINT_ID), TITLE_ID));

	std::tstring strName;
	std::tstring strNote;
	int nIdRecord = 0;
	IDatabase* pDb = IApplication::GetApp()->GetDatabaseManager()->OpenDatabase(JOURNAL_DATABASE_NAME);
	CPointsTable points;
	points.m_content.s_nId = m_nIdParameter;
	IResult* pResult = points.GetValue(pDb);
	if(pResult->GetSizeResult()){
		strName = ((CPointsTable::content*)(*pResult)[0])->s_strName;
		strNote = ((CPointsTable::content*)(*pResult)[0])->s_strNote;
		nIdRecord = ((CPointsTable::content*)(*pResult)[0])->s_nIdRecord;
	}
	pResult->Free();
	IApplication::GetApp()->GetDatabaseManager()->CloseDatabase(pDb);
	m_editorDrawer.AddItem(new CEditItem(IApplication::GetApp()->GetLanguageManager()->GetString(TABLE_HEADER_POINT_NAME_ID), NAME_POINT_ID, editItem::TEXT, strName));
	CEditorItem* pItemNote = new CEditItem(IApplication::GetApp()->GetLanguageManager()->GetString(TABLE_HEADER_NOTE_ID), NOTE_POINT_ID, editItem::TEXT, strNote);
	m_editorDrawer.AddItem(pItemNote);
	CPointViewSettings pSettings;
	if(pSettings.GetState(CHECK_NOTE_ID) != CHECK_BOX_STATE_1)
		pItemNote->Hide(true);

	CViewFieldValuesHelper helper(&m_editorDrawer);
	m_nUserFields = helper.GenerateUserEditItems(CViewFieldValuesHelper::POINTS, USER_FIELD_ID, nIdRecord);
}

void CChangePointView::SetState(){
	m_pState->m_nState = JournalEnum::VIEW_CHANGE_POINT;
}
bool CChangePointView::IsViewState(State state){
	return state.m_type == JournalEnum::CHANGE_POINT;
}

BEGIN_MESSAGE_MAP(CChangePointView, CAddChangeView)
	ON_COMMAND(BUTTON_SAVE_ID, OnSavePoint)
END_MESSAGE_MAP()

void CChangePointView::OnSavePoint(){
	IDatabase* pDb = IApplication::GetApp()->GetDatabaseManager()->OpenDatabase(JOURNAL_DATABASE_NAME);
	CChangePointOperation operation;
	operation.m_content.s_nId = m_nIdParameter;
	operation.m_content.s_strName = m_editorDrawer.GetValueItem(NAME_POINT_ID);
	operation.m_content.s_strNote = m_editorDrawer.GetValueItem(NOTE_POINT_ID);
	for(int i = 1; i <= m_nUserFields; i++)
		operation.m_content.s_aUserFields.insert(std::make_pair(m_editorDrawer.GetNameItem(USER_FIELD_ID + i), m_editorDrawer.GetValueItem(USER_FIELD_ID + i)));
	operation.UpdateValue(pDb);
	IApplication::GetApp()->GetDatabaseManager()->CloseDatabase(pDb);
	__super::OnCancel();
}